package com.slipper.scheduled;

import java.util.List;

import com.slipper.system.schedule.service.ScheduleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jakarta.annotation.Resource;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

import com.slipper.system.schedule.bo.ScheduleBO;
import org.springframework.stereotype.Component;

@Component
public class ScheduledRunner implements ApplicationRunner {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private ScheduleService scheduleService;

    @Override
    public void run(ApplicationArguments args) {
        ScheduleBO bo = new ScheduleBO();
        bo.setStatus(ScheduleBO.STATUS_NORMAL);
        List<ScheduleBO> scheduleList = this.scheduleService.queryList(bo);

        for (ScheduleBO scheduleBO : scheduleList) {
            try {
                logger.info("初始化调度：{}", scheduleBO);
                ScheduledUtil.createScheduleJob(scheduleBO);
            } catch (Exception e) {
                logger.error("创建定时任务调度失败！", e);
            }
        }
        logger.info("调度已加载 {} 条...", scheduleList.size());
    }
}
